<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<html>
	<head>
		<title>
			Add_sql
		</title>
	</head>
	<body>
		<%out.println(" " + session.getAttribute("section") + " "
					+ session.getAttribute("grade"));
	    try{
	    String task_no=(String)session.getAttribute("task_no");
	    String domain=(String)session.getAttribute("domain");
		String type=(String)session.getAttribute("type");
		String grade=(String)session.getAttribute("grade");
		String state=(String)session.getAttribute("state");
		String section=(String)session.getAttribute("mger_type");
		String mailtitle=(String)session.getAttribute("Mailtitle");
		String mailcontent=(String)session.getAttribute("Mailcontent");
		String areacode=(String)session.getAttribute("areacode");
		if(task_no==null||domain==null||type==null||grade==null||state==null||section==null||mailtitle==null||mailcontent==null){
		response.sendRedirect("GroupSendError.jsp");
		}else{
			Context initCtx = new InitialContext();
			Context envCtx = (Context) initCtx.lookup("java:comp/env");
			DataSource ds = (DataSource) envCtx.lookup("jdbc/TestDB");
			Connection conn = ds.getConnection();
			Statement stmt = conn.createStatement();
			String SQL_task = "insert into Bat_sendmsg_task(task_no,area_code,staff_no,send_type,mail_title,mail_content,createuser_sum,state)values('"
					+ session.getAttribute("task_no")
					+ "','"
					+ session.getAttribute("mger_type")
				    + "','"
				    +session.getAttribute("mger_user")
				    +"','MAIL','"
				    +session.getAttribute("Mailtitle")
				    +"','"
				    +session.getAttribute("Mailcontent")
				    +"',"
				    +session.getAttribute("count")+",'INIT')";
		   String SQL_num= "insert into bat_sendmsg_num(username,task_no,send_result)select username,'"+task_no+"','INIT' from users where username like '"+section+"%'";
		   
		   if(!grade.equals("10")){
		   SQL_num+=" and grade="+Short.parseShort(grade);
		   }
		   if(!state.equals("10")){
		   SQL_num+=" and state="+Short.parseShort(state);
		   }
		   if(!type.equals("10")){
		   SQL_num+=" and type='"+type+"'";
		   }
		   if(areacode!=null){
		   SQL_num+=" and username like '"+areacode+"%'";
		   }
		  // out.print("--------"+SQL_num);
			stmt.executeUpdate(SQL_task);
			stmt.executeUpdate(SQL_num);
			stmt.close();
			conn.close();
			response.sendRedirect("GroupMailParticular.jsp?task_no="+task_no);
			}
          }catch(Exception e)
          {
          response.sendRedirect("GroupSendError.jsp");
          }
		%>
	</body>
</html>
